$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'mercharReport/storeproductprice/list',
        datatype: "json",
        colModel: [			
			{ label: '产品名称', name: 'productName', index: 'productName', width: 50 },
			{ label: '规格属性', name: 'craft', index: 'craft', width: 110 },
			{ label: '单价', name: 'price', index: 'price', width: 50 ,align:'center'},
			{ label: '起步价', name: 'startPrice', index: 'startPrice', width: 50 ,align:'center'},
			{ label: '阶梯价', name: 'stepPrice', index: 'step_price', width: 110,
                formatter: function (value, options, row) {
                    return value.replace(/\[/ig,'').replace(/\]/ig,'').replace(/{/ig,'').replace(/}/ig,'').replace(/"/ig,'').replace(/minSum/ig,'最少').replace(/maxSum/ig,'最多').replace(/price/ig,'价格')
                }},
			{ label: '备注', name: 'marks', index: 'marks', width: 50 ,align:'center'},
            { label: '创建人', name: 'createName', index: 'create_name', width: 80 ,align:'center'},
            { label: '创建时间', name: 'createTime', index: 'create_time', width: 80 },
            { label: '更新人', name: 'updateName', index: 'update_name', width: 80 ,align:'center'},
			{ label: '更新时间', name: 'updateTime', index: 'update_time', width: 80 }, 			
        ],
		viewrecords: true,
        height: 700,
        rowNum: 25,
		rowList : [25,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount",
            userdata: "userData"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" });
            vm.selfShopEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').selfShopEnum;
            vm.goodsTypeEnum = $("#jqGrid").jqGrid('getGridParam', 'userData').goodsTypeEnum;
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		title: null,
		storeProductPrice: {},
		q:{
            productName:'',
            sellerId:'',
            goodsType:''
		},
        selfShopEnum:[],
        goodsTypeEnum:[]
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.storeProductPrice = {};
		},
		update: function (event) {
			var stepId = getSelectedRow();
			if(stepId == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            
            vm.getInfo(stepId)
		},
		saveOrUpdate: function (event) {
			var url = vm.storeProductPrice.stepId == null ? "mercharReport/storeproductprice/save" : "mercharReport/storeproductprice/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.storeProductPrice),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		del: function (event) {
			var stepIds = getSelectedRows();
			if(stepIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "mercharReport/storeproductprice/delete",
                    contentType: "application/json",
				    data: JSON.stringify(stepIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(stepId){
			$.get(baseURL + "mercharReport/storeproductprice/info/"+stepId, function(r){
                vm.storeProductPrice = r.storeProductPrice;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{
                postData: {//postData此数组内容直接赋值到url上，参数类型：{name1:value1…}
                    'productName': vm.q.productName.trim(),
                    'sellerId': vm.q.sellerId,
                    'goodsType': vm.q.goodsType,
                },
                page:1
            }).trigger("reloadGrid");
		},
        reset: function (event) {
            vm.q.productName = '';
            vm.q.sellerId = '';
            vm.q.goodsType = '';
            vm.reload();
        }
	}
});